/****************************************************
 * Description: Controllar for 用户组角色
 * Copyright:   Copyright (c) 2013
 * Company:     beiwaionline
 * @author      bfsu
 * @version     1.0
 * @see
	HISTORY
	*  2013-08-08 bfsu Create File
**************************************************/

package com.bfsuolframework.security.controllar;




import com.bfsuolframework.security.annotations.SecFunction;
import com.bfsuolframework.security.entity.Group;
import com.bfsuolframework.security.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bfsuolframework.security.entity.GroupRole;
import com.bfsuolframework.security.service.RoleService;
import com.bfsuolframework.security.service._GroupRoleService;
import com.bfsuolframework.core.BfsuolConstants;
import com.bfsuolframework.core.controller.SpringControllerSupport;
import com.bfsuolframework.core.controller.json.MessageJson;
import com.bfsuolframework.core.controller.support.Pagination;
import com.bfsuolframework.core.controller.support.QueryParam;
import com.bfsuolframework.core.exception.ValidationException;
import com.bfsuolframework.core.spring.QueryParameter;

@Controller
@RequestMapping("/components/security/groupRole")
public class GroupRoleController extends SpringControllerSupport {

    @Autowired
    private _GroupRoleService groupRoleService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private GroupService groupService;

    @SecFunction(title="角色设置",privilege="components.security.group",code="roleSetting")
	@RequestMapping("/index/{groupId}")
	public String index(
			@PathVariable("groupId") Long groupId,
			Model model){
		Group group = groupService.getById(groupId);
		model.addAttribute("group", group);
		return getViewNamedPath("index");
	}
    @SecFunction(title="角色设置",privilege="components.security.group",code="roleSetting")
	@RequestMapping("/list/{groupId}")
	public String list(
			@PathVariable("groupId") Long groupId,
			@QueryParameter("query") QueryParam query,
			@ModelAttribute("page") Pagination page,
			Model model){
		query.addQuery("group.id!eq@l", groupId);
		page = groupRoleService.getPage(query, page);
		model.addAttribute("isdefault", BfsuolConstants.GLOBAL_YESNO_YES);
		return getViewNamedPath("list");
	}
    @SecFunction(title="角色设置",privilege="components.security.group",code="roleSetting")
	@RequestMapping("/input")
	public String create(
			@QueryParameter("query") QueryParam query,
			@ModelAttribute("page") Pagination page,Model model){
		
		page = roleService.getPage(query, page);
		
		return getViewNamedPath("input");
	}
    @SecFunction(title="角色设置",privilege="components.security.group",code="roleSetting")
	@RequestMapping("/input/{id}")
	public String edit(@PathVariable("id") Long id, Model model){
		GroupRole groupRole = groupRoleService.getById(id);
		model.addAttribute(groupRole);
		return getViewNamedPath("input");
	}
    @SecFunction(title="角色设置",privilege="components.security.group",code="roleSetting")
	@RequestMapping("/save")
	public @ResponseBody MessageJson save(@ModelAttribute GroupRole groupRole){
		if(groupRole.isNew()){
			groupRoleService.save(groupRole);
		}else{
			groupRoleService.update(groupRole);
		}
		return MessageJson.information("保存成功");
	}
	
    @SecFunction(title="角色设置",privilege="components.security.group",code="roleSetting")
	@RequestMapping("/delete/{id}")
	public @ResponseBody MessageJson delete(@PathVariable("id") Long id){
		groupRoleService.delete(id);
		return MessageJson.information("成功从用户组中删除1个角色");
	}
    @SecFunction(title="角色设置",privilege="components.security.group",code="roleSetting")
	@RequestMapping("/delete")
	public @ResponseBody MessageJson delete(@RequestParam("ids") Long[] ids){
		if(ids == null || ids.length == 0){
			throw new ValidationException("没有删除");
		}
		for(Long id : ids){
			groupRoleService.delete(id);
		}
		return MessageJson.information("成功从用户组中删除"+ids.length+"个角色");
	}
    @SecFunction(title="角色设置",privilege="components.security.group",code="roleSetting")
	@RequestMapping("/setDefault/{id}")
	public @ResponseBody MessageJson setDefault(@PathVariable("id") Long id){
		
		GroupRole gr = groupRoleService.getById(id);
		gr.setIsDefault(BfsuolConstants.GLOBAL_YESNO_YES);
		groupRoleService.update(gr);
		return MessageJson.success("设置默认成功");
	}
    @SecFunction(title="角色设置",privilege="components.security.group",code="roleSetting")
	@RequestMapping("/romoveDefault/{id}")
	public @ResponseBody MessageJson romoveDefault(@PathVariable("id") Long id){
		
		GroupRole gr = groupRoleService.getById(id);
		gr.setIsDefault(BfsuolConstants.GLOBAL_YESNO_NO);
		groupRoleService.update(gr);
		return MessageJson.success("取消默认成功");
	}
    @SecFunction(title="角色设置",privilege="components.security.group",code="roleSetting")
	@RequestMapping("/addRole/{groupId}")
	public @ResponseBody MessageJson addRole(
			@PathVariable("groupId") Long groupId,
			@RequestParam("roleIds") Long[] roleIds){
		if(roleIds != null && roleIds.length > 0){
			Group group = groupService.getById(groupId);
			groupService.save(group,roleIds);
			return MessageJson.success("成功添加"+roleIds.length+"个角色");
		}else{
			throw new ValidationException("没有要添加的角色");
		}
		
	}
}
